package com.zhixing.reward.listener;

import com.zhixing.common.domain.entity.UserInfo;
import com.zhixing.common.domain.enums.Role;
import com.zhixing.reward.service.CouponService;
import com.zhixing.reward.service.RewardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import static com.zhixing.common.constant.Constant.*;

@Slf4j
@Component
@RequiredArgsConstructor
public class RewardListener {

    private final CouponService couponService;
    private final RewardService rewardService;

    @KafkaListener(topics = ZHIXING_USER_REGISTER, groupId = ZHIXING_REWARD_NEWCOMER)
    public void newcomerReward(UserInfo userInfo) {
        log.info("奖励服务调用，新人发放优惠券或开启奖励，用户id：{}", userInfo.getId());
        if (userInfo.getRole() == Role.PASSENGER) couponService.sendNewcomerCoupon(userInfo.getId());
        else rewardService.sendNewcomerReward(userInfo.getId());
    }
}
